package fr.labri.macchiato.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.storage.client.Storage;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.util.SC;

import fr.labri.macchiato.client.preferences.PreferencesPrompt;
import fr.labri.macchiato.client.project.ProjectWidget;
import fr.labri.macchiato.framework.business.model.project.Project;
import fr.labri.macchiato.framework.business.service.ProjectService;
import fr.labri.macchiato.framework.business.service.Services;
import fr.labri.macchiato.framework.technical.queryLauncher.RpcQueryLauncher;
import fr.labri.macchiato.framework.technical.queryLauncher.RpcQueryLauncherAsync;
import fr.labri.macchiato.framework.technical.share.ShareFunctionalities;
import fr.labri.macchiato.framework.util.FrameworkException;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Macchiato implements EntryPoint {

	private static final String DEFAULT_CART_NAME = "Ma liste de courses";
	private static final String DEFAULT_PROJECT_NAME = "Mes courses de la semaine";
	private static final double DEFAUT_PROJECT_BUDGET = 100;
	private static final double DEFAULT_CART_BUDGET = 10;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {

		if (!Storage.isSupported())
			SC.say("Les fonctionnalités de stockage HTML5, essentielles à cette application, ne sont pas supportées par votre navigateur");
		else {

			loadModel();
			Services services = Services.getInstance();
			// we first initialize the service factory using the local storage,
			// to check if the preferences have been configured
			services.initializeServiceFactory(true);
			if (services.getPreferencesService().arePreferencesConfigured()) {
				boolean useLocalStorage = services.getPreferencesService()
						.getUseLocalStorage();
				if (!useLocalStorage)
					// if we use the local storage, there is no need to
					// initialize the factory again
					services.initializeServiceFactory(useLocalStorage);
				initApplicationPanel();
			} else
				showPreferencesPrompt();

		}
	}

	/**
	 * Initializes the application panel. If there is no existing project, the
	 * default project is create with a cart
	 */
	public static void initApplicationPanel() {
		ApplicationPanel main = new ApplicationPanel();
		String projectName;

		Project newProject = null;

		try {
			newProject = ShareFunctionalities.getSharedProject();
		} catch (FrameworkException e) {
			SC.warn(e.getLocalizedMessage());
		}

		if (newProject != null) {
			Services.getInstance().getProjectService().saveProject(newProject);
		} else {

			List<Project> projects = getProjectService().getAllProjects();

			if (projects == null || projects.size() == 0) {
				projectName = DEFAULT_PROJECT_NAME;
				newProject = getProjectService().newProject(projectName,
						DEFAUT_PROJECT_BUDGET);
				getProjectService().addCategory(projectName, DEFAULT_CART_NAME,
						DEFAULT_CART_BUDGET);
			} else {
				newProject = projects.get(0);
			}
		}
		main.setProjectWidget(new ProjectWidget(newProject.getName()));

		main.draw();
	}

	private static ProjectService getProjectService() {
		return Services.getInstance().getProjectService();

	}

	private void showPreferencesPrompt() {
		PreferencesPrompt prompt = new PreferencesPrompt();
		prompt.show();
	}

	private void loadModel() {
		RpcQueryLauncherAsync queryService = GWT
				.create(RpcQueryLauncher.class);
		queryService.loadModel(new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
			}
		});
	}

}
